package com.bustle.user.dto.address;

import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.annotation.XmlRootElement;

@Path("/address/")
@Produces(
  {
    MediaType.APPLICATION_XML
  })
@Stateless
public class AddressDtoDaoWebService
{
  @POST
  @Path("/create")
  public AddressDto create(AddressDto anAddressDto)
  {
    addressDtoDao.create(anAddressDto);
    return anAddressDto;
  }

  @GET
  @Path("/retrieve")
  public AddressDto retrieve(@QueryParam("key") Integer aKey)
  {
    return addressDtoDao.retrieve(aKey);
  }

  @GET
  @Path("/retrieveAll")
  public AddressDtoList retrieveAll()
  {
    AddressDtoList all = new AddressDtoList();
    all.setAddress(addressDtoDao.retrieveAll());
    return all;
  }

  @PUT
  @Path("/update")
  public Response update(AddressDto anAddressDto)
  {
    addressDtoDao.update(anAddressDto);
    return Response.status(Status.OK).build();
  }

  @DELETE
  @Path("/delete/{id}")
  public Response delete(@PathParam("id") Integer aKey)
  {
    addressDtoDao.delete(aKey);
    return Response.status(Status.OK).build();
  }

  @EJB
  protected AddressDtoDao addressDtoDao;

  @XmlRootElement(name = "addresses")
  static public class AddressDtoList
  {
    public List<AddressDto> getAddress()
    {
      return address;
    }

    public void setAddress(List<AddressDto> aList)
    {
      address = aList;
    }

    protected List<AddressDto> address = new Vector<>();
  }
}
